<?php

namespace services\marketing;

use common\components\Service;
use common\helpers\CommonHelper;
use common\models\marketing\OutpatientType;
use common\models\patient\Callback;
use common\models\patient\User;

/**
 * Class RepairOutpatientTypeService
 * @package services\marketing
 */
class RepairOutpatientTypeService extends Service
{
    /**
     * 修复就诊类型数据
     *
     * @return array
     */
    public static function updateOutpatientType()
    {
        $outpatientTypeAll = self::outpatientType();
        foreach ($outpatientTypeAll['new'] as $val) {
            User::updateAll(
                [
                    'outpatient_type_id' => $val['id'],
                ],
                [
                    'outpatient_type_id' => $outpatientTypeAll['old'][$val['id']],
                ]
            );
            Callback::updateAll(
                [
                    'outpatient_type_id' => $val['id'],
                ],
                [
                    'outpatient_type_id' => $outpatientTypeAll['old'][$val['id']],
                ]
            );
        }

        OutpatientType::updateAll(['project_id' => 0], ['project_id' => 1]);// 修改就诊类型项目ID为0
        self::delOutpatientType();// 删除其他就诊类型
        return true;
    }

    /**
     * 新老数据对应关系
     */
    public static function outpatientType()
    {
        $old = [
            1000 => [1009, 1014, 1019, 1024, 1029, 1034, 1035, 1040, 1049, 1054, 1055, 0],
            1001 => [1008, 1013, 1018, 1023, 1028, 1033, 1036, 1041, 1048, 1053, 1056],
            1002 => [1007, 1012, 1017, 1022, 1027, 1032, 1037, 1042, 1047, 1052, 1057],
            1003 => [1006, 1011, 1016, 1021, 1026, 1031, 1038, 1043, 1046, 1051, 1058],
            1004 => [1005, 1010, 1015, 1020, 1025, 1030, 1039, 1044, 1045, 1050, 1059],
        ];

        $new = [
            [
                'id' => 1000,
                'name' => '初诊',
            ],
            [
                'id' => 1001,
                'name' => '复诊',
            ],
            [
                'id' => 1002,
                'name' => '流失',
            ],
            [
                'id' => 1003,
                'name' => '断诊',
            ],
            [
                'id' => 1004,
                'name' => '咨询号',
            ],
        ];

        return ['new' => $new, 'old' => $old];
    }

    /**
     * 删除其他就诊类型
     */
    public static function delOutpatientType()
    {
        return OutpatientType::deleteAll(['>', 'id', 1004]);
    }


}
